package com.ltedu.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ltedu.common.contants.Constants;
import com.ltedu.common.vo.Result;
import com.ltedu.user.domain.Teacher;
import com.ltedu.user.domain.vo.TeacherVO;
import com.ltedu.user.service.TeacherService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师表 前端控制器
 * </p>
 *
 * @author Jerry
 * @since 2020-11-26
 */
@RestController
@RequestMapping("/user/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 获取所有有效状态讲师
     *
     * @return {@link Result}
     */
    @GetMapping("/all")
    public Result all() {
        return Result.ok(teacherService.list(new LambdaQueryWrapper<Teacher>().
                eq(Teacher::getState, Constants.NORMAL)));
    }

    /**
     * 分页查询讲师列表
     *
     * @param page        页面
     * @param limit       限制
     * @param teacherName 老师的名字
     * @param level       水平
     * @param state       状态
     * @return {@link Result}
     */
    @GetMapping("/list/{page}/{limit}")
    public Result list(@PathVariable Integer page,
                       @PathVariable Integer  limit,
                       @RequestParam(value = "teacherName", required = false) String teacherName,
                       @RequestParam(value = "level", required = false) Integer level,
                       @RequestParam(value = "state", required = false) Integer state) {
        Page<Teacher> pageParam = new Page<>(page, limit);
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(teacherName)) {
            queryWrapper.like(Teacher::getName, teacherName);
        }
        if (level != null) {
            queryWrapper.like(Teacher::getLevel, level);
        }
        if (state != null) {
            queryWrapper.like(Teacher::getState, state);
        }
        return Result.ok(teacherService.page(pageParam, queryWrapper));
    }

    /**
     * 根据id获取讲师信息
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        return Result.ok(teacherService.getById(id));
    }

    /**
     * 添加讲师
     *
     * @param teacherVO
     * @return {@link Result}
     */
    @PostMapping("/add")
    public Result add(@RequestBody TeacherVO teacherVO) {
        return Result.ok(teacherService.add(teacherVO));
    }

    /**
     * 更新讲师
     *
     * @param teacherVO
     * @return {@link Result}
     */
    @PutMapping("/update")
    public Result update(@RequestBody TeacherVO teacherVO) {
        return Result.ok(teacherService.update(teacherVO));
    }

    /**
     * 根据id删除讲师
     *
     * @param id id
     * @return {@link Result}
     */
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        return Result.ok(teacherService.removeById(id));
    }
}

